package com.miplus.ccrm.core.service.cust;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miplus.ccrm.common.EscResVo;
import com.miplus.ccrm.core.mapper.FiCustomerScorecardHistMapper;
import com.miplus.ccrm.core.mapper.FiCustomerScorecardMapper;
import com.miplus.ccrm.core.pojo.FiCustomerScorecard;
import com.miplus.ccrm.core.pojo.FiCustomerScorecardHist;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author Yaohui
 * @description 针对表【fi_customer_scorecard(客户C卡计分表)】的数据库操作Service实现
 * @createDate 2025-06-08 14:08:44
 */
@Slf4j
@Service
public class FiCustomerScorecardServiceImpl extends ServiceImpl<FiCustomerScorecardMapper, FiCustomerScorecard>
        implements FiCustomerScorecardService{

    @Resource
    FiCustomerScorecardHistMapper customerScorecardHistMapper;

    @Override
    public EscResVo cafp0001ResultHandle(JSONObject jsonObject) {
        EscResVo resVo=new EscResVo();
        Date currentDate =new Date();

        if(jsonObject.containsKey("Body")){

            JSONObject body = JSONObject.from(jsonObject.get("Body"));
            log.info("body:{}",body);
            if(!body.containsKey("RskId")){
                resVo.setExecSt(0);
                resVo.setDscMsg("参数:RskId缺失");
                return resVo;
            }
            if(!body.containsKey("RskRst")){
                resVo.setExecSt(0);
                resVo.setDscMsg("参数:RskRst缺失");
                return resVo;
            }

            String rskId = (String) body.get("RskId");
            FiCustomerScorecardHist dbApply = customerScorecardHistMapper.selectOne(new QueryWrapper<FiCustomerScorecardHist>()
                    .eq("apply_id", rskId
                    ));

            if(dbApply==null){
                resVo.setExecSt(0);
                resVo.setDscMsg("错误的RskId");
                return resVo;
            }

            JSONObject rskRst = body.getJSONObject("RskRst");
            log.info("rskRst:{}",rskRst);

            if((Boolean)rskRst.get("success")){
                //调用成功 不能新建。20250613
                //dbApply=new FiCustomerScorecardHist();
                JSONObject data= rskRst.getJSONObject("data");
                log.info("data:{}",data);

                dbApply.setReasonCode(data.getInteger("reasonCode"));
                dbApply.setReasonDesc(data.getString("reasonDesc"));
                dbApply.setWorkflowCode(data.getString("workflowCode"));
                dbApply.setWorkflowVersion(data.getInteger("workflowVersion"));
                dbApply.setProductCode(data.getString("productCode"));
                dbApply.setFinalDealTypeCode(data.getString("finalDealTypeCode"));


                JSONObject envVariable = data.getJSONObject("envVariable");
                dbApply.setRiskCode(envVariable.getString("riskCode"));
                dbApply.setRiskCodeDetail(envVariable.getString("riskCodeDetail"));
                dbApply.setCCardScore(envVariable.getDouble("cCardScore"));
                dbApply.setCustomerLevel(envVariable.getString("customerLevel"));
                dbApply.setIsStop(envVariable.getString("isStop"));
                dbApply.setIsStopReason(envVariable.getString("isStopReason"));
                dbApply.setFeature1(envVariable.getString("feature1"));
                dbApply.setFeature2(envVariable.getString("feature2"));
                dbApply.setFeature3(envVariable.getString("feature3"));
                dbApply.setFeature4(envVariable.getString("feature4"));
                dbApply.setFeature5(envVariable.getString("feature5"));
                dbApply.setOtherFeature(envVariable.getString("otherFeature"));
                log.info("dbApply:{}",dbApply);

                dbApply.setStatus("R");
                dbApply.setMddt(currentDate);
                dbApply.setMdusr("system");
                dbApply.setResonDt( new Date());
                int i = customerScorecardHistMapper.updateById(dbApply);
                if(i>0 && dbApply.getCCardScore().compareTo(-9999.0) != 0){
                    FiCustomerScorecard customerScorecard=new FiCustomerScorecard();
                    BeanUtils.copyProperties(dbApply,customerScorecard);
                    log.info("customerScorecard:{}",customerScorecard);
                    saveOrUpdate(customerScorecard);
                }

            }else{
                FiCustomerScorecardHist scorecardHist=new FiCustomerScorecardHist();
                scorecardHist.setScoreid(dbApply.getScoreid());
//                scorecardHist.setApplyId(rskId);
                scorecardHist.setStatus("F");
                scorecardHist.setMddt(currentDate);
                scorecardHist.setMdusr("system");

//                scorecardHist.setReasonCode();
//                scorecardHist.setReasonDesc();
                scorecardHist.setReasonText(body.toJSONString());
                //对方返回失败
                customerScorecardHistMapper.updateById(scorecardHist);
            }

        }


        return new EscResVo(1,"ok");
    }
}


